﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Robber : MonoBehaviour
{
    public EPlayerType PlayerType;
    public SpriteRenderer MySprite;
    public SpriteRenderer MouthSprite;
    public Rigidbody2D MyRigidBody;
    public CapsuleCollider2D MyCollider;
    public GameObject MouthPosition;
    public Animator MyAnimator;

    private const float NormalHeight = 1.5f;
    private const float StretchHeight = 2.5f;
    private const float CrouchHeight = 1.3f;
    private const float AddForce = 3f;
    private const float TextAddForce = 6f;
    private const float MaxVelocity = 5f;
    private const float JumpForce = 250f;
    private const float UpStateBlendTime = 1.2f;
    private const float DownStateBlendTime = 0.5f;
    private static float[] HeightArray = new float[] { NormalHeight, StretchHeight, CrouchHeight };
    private const float ColliderXScale = 0.84f / NormalHeight;
    private const float ColliderYScale = 1.35f / NormalHeight;
    private const float PlayerHeadOffset = 0.1f;

    private KeyCode mUpKeyCode;
    private KeyCode mDownKeyCode;
    private KeyCode mLeftKeyCode;
    private KeyCode mRightKeyCode;
    private KeyCode mJumpKeyCode;
    private EPlayerState mCurrentState;
    private EPlayerState mLastState;
    private float mChangeStateTime;
    private float mInAirTime;
    private float mLastJumpTime;

    private string HoriAxis;
    private string VertiAxis;
    private string JumpAxis;
    private List<TextScript> StoledText = new List<TextScript>();
    public bool OnlyCanCrouch
    {
        get;
        set;
    }
    void Start()
    {
        mCurrentState = EPlayerState.Normal;
    }

    void Update()
    {
        MyRigidBody.AddForce(Vector2.left * AddForce);
        bool isGrounded = MyRigidBody.IsTouchingLayers(1 << PhysicsLayer.Floor);
        if (!isGrounded)
        {
            mInAirTime += Time.deltaTime;
        }
        else
        {
            mInAirTime = 0f;
        }
        MyAnimator.SetBool("IsGrounded", isGrounded);
        MyAnimator.SetFloat("InAirTime", mInAirTime);
        if (MyRigidBody.velocity.x >= 0 ^ PlayerType == EPlayerType.P2)
        {
            MySprite.flipX = false;
            MouthSprite.flipX = false;
        }
        else
        {
            MySprite.flipX = true;
            MouthSprite.flipX = true;
        }
    }
    private void ChangeState(EPlayerState newState)
    {
        if (newState != mCurrentState)
        {
            mLastState = mCurrentState;
            mCurrentState = newState;
            mChangeStateTime = Time.time;
        }
    }
    private void FixedUpdate()
    {
        if (mCurrentState == EPlayerState.Stretching)
        {
            if (Time.time - mChangeStateTime <= UpStateBlendTime)
            {
                float percent = (Time.time - mChangeStateTime) / UpStateBlendTime;
                float height = Mathf.Lerp(MySprite.size.y, HeightArray[(int)mCurrentState], percent);
                Vector2 size = MySprite.size;
                size.y = height;
                MySprite.size = size;
                MyCollider.size = new Vector2(size.x * ColliderXScale, size.y * ColliderYScale);

                float deltaHeight = height - NormalHeight;
                Vector3 mouthPosition = MouthPosition.transform.localPosition;
                mouthPosition.y = deltaHeight - (height / 2f) + NormalHeight / 2f;
                MouthPosition.transform.localPosition = mouthPosition;
            }
        }
        else
        {
            if (Time.time - mChangeStateTime <= DownStateBlendTime)
            {
                float percent = (Time.time - mChangeStateTime) / DownStateBlendTime;
                float height = Mathf.Lerp(MySprite.size.y, HeightArray[(int)mCurrentState], percent);
                Vector2 size = MySprite.size;
                size.y = height;
                MySprite.size = size;
                MyCollider.size = new Vector2(size.x * ColliderXScale, size.y * ColliderYScale);

                float deltaHeight = height - NormalHeight;
                Vector3 mouthPosition = MouthPosition.transform.localPosition;
                mouthPosition.y = deltaHeight - (height / 2f) + NormalHeight / 2f;
                MouthPosition.transform.localPosition = mouthPosition;
            }
        }
        foreach(var script in StoledText)
        {
            script.transform.position = GetHeadPosition();
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == PhysicsLayer.Player)
        {
            Player player = collision.gameObject.GetComponent<Player>();
            if (player.GetState() != EPlayerState.Stretching)
            {
                TextScript script = GameManager.GetInstance().StoleAText();
                if (script != null)
                {
                    script.transform.position = GetHeadPosition();
                    script.transform.parent = transform;
                    script.GetComponent<Rigidbody2D>().velocity = Vector3.zero;
                    script.GetComponent<Collider2D>().enabled = false;
                    script.GetComponent<Rigidbody2D>().isKinematic = true;
                    script.GetComponent<Rigidbody2D>().Sleep();
                    StoledText.Add(script);
                }
            }
        }
    }
    public Vector3 GetHeadPosition()
    {
        return transform.position + Vector3.up * (MySprite.size.y / 2f * transform.localScale.x + PlayerHeadOffset);
    }
    public EPlayerState GetState()
    {
        return mCurrentState;
    }
}
